﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApp.BaseDataSvc;
using YY.Common;

namespace WebApp
{
    public partial class EditWorkFlowFrm : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(EditWorkFlowFrm), this);
            var orderId = Request.Params["OrderId"];
            if (string.IsNullOrEmpty(orderId))
            {
                Response.Write("非法进入");
                Response.End();
            }
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<TB_Workflow> GetWF(int orderId)
        {
            AjaxResult<TB_Workflow> result = new AjaxResult<TB_Workflow>();
            result.Data = null;
            result.Msg = null;
            try
            {
                var order = this.BaseDataProxy.GetOrder(orderId);
                result.Data = this.BaseDataProxy.GetCurrentWorkflow(order);
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> ConfirmWF(int orderId, int op, string comment)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Handle, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                TB_User user = Session["User"] as TB_User;
                if (op != user.id)
                {
                    result.Msg = "不是环节处理人没有处理权限！";
                    return result;
                }

                MsgBoolean mb = this.BaseDataProxy.HandleWorkflow2(orderId, 0, null, null, comment, user.id, user.role);
                result.Data = mb.Ok;
                if (!mb.Ok)
                    result.Msg = mb.Msg;
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> LZWF(int orderId, int nextOp, bool isNext, string comment)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Handle, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                TB_User user = Session["User"] as TB_User;
                Type t = typeof(EnumCommon.OrderStatus);
                var order = this.BaseDataProxy.GetOrder(orderId);
                var wf = this.BaseDataProxy.GetCurrentWorkflow(order);
                int current = (int)Enum.Parse(t, wf.flowStatus);
                int next = current + 1;
                int opType = 1;
                if (!isNext)
                {
                    opType = -1;
                    next = current - 1;
                }

                MsgBoolean mb = this.BaseDataProxy.HandleWorkflow2(orderId, opType, nextOp, Enum.GetName(t, next), comment, user.id, user.role);
                result.Data = mb.Ok;
                if (!mb.Ok)
                    result.Msg = mb.Msg;
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

    }
}
